from django.shortcuts import render
from django.contrib import auth
# Create your views here.
# 登录


def login(request):
    if request.method == 'POST':
        username = request.POST.get("username")
        password = request.POST.get("password")
        user = auth.authenticate(username=username, password=password)
        if user is not None:
            auth.login(request, user)
            request.session["user"] = username
            return render(request, 'index.html')
        else:
            return render(request, 'login.html', context={"error": "用户名或密码错"})
    else:
        return render(request, 'login.html')
